package com.aiolos.weather.job;

import com.aiolos.weather.service.CityDataService;
import com.aiolos.weather.service.WeatherDataService;
import com.aiolos.weather.vo.City;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.List;

/**
 * 同步天气数据的定时任务类
 */

@Slf4j
public class WeatherDataSyncJob extends QuartzJobBean {

    @Autowired
    private CityDataService cityDataService;

    @Autowired
    private WeatherDataService weatherDataService;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("Weather Data Sync Job starting...");
        //1. 获取城市id列表
        List<City> cityList = null;
        try {
            cityList = cityDataService.getCityList();
        } catch (Exception e) {
            log.error("Exception!", e);
        }
        //2. 遍历列表，获取城市天气数据并保存到Redis中
        for (City city:cityList) {
            String cityId = city.getCityId();
            log.info("Weather Data Sync Job, cityId:"+cityId);
            weatherDataService.syncWeatherDataByCityId(cityId);
        }

        log.info("Weather Data Sync Job finished!");
    }
}
